package cn.snorlaxzjt.LBSserver.service;


import cn.snorlaxzjt.LBSserver.mapper.FriendsMapper;
import cn.snorlaxzjt.LBSserver.mapper.UserMapper;
import cn.snorlaxzjt.LBSserver.po.body.FriendBody;
import cn.snorlaxzjt.LBSserver.po.entity.Friends;
import cn.snorlaxzjt.LBSserver.po.entity.User;
import cn.snorlaxzjt.LBSserver.po.page.ResultPage;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Transactional(rollbackFor = RuntimeException.class)
@Slf4j
@Service
public class FriendsService
{

    @Autowired
    FriendsMapper friendsMapper;

    @Autowired
    UserMapper userMapper;

    public List<Integer> getMyFriends(int id)
    {
        return friendsMapper.getFriendsById(id);
    }

    public List<Integer> getMyFans(int id)
    {
        return friendsMapper.getFansById(id);
    }

    public int follow(Friends friends)
    {
        return friendsMapper.insert(friends);
    }

    public int cancelFollow(Integer userId, Integer friendId)
    {
        Map<String, Object> map=new HashMap<>();
        map.put("user_id",userId);
        map.put("friend_id",friendId);
        return friendsMapper.deleteByMap(map);
    }

    public void initReg(int id){
        Friends f = new Friends();
        f.setUserId(id);
        f.setFriendId(id);
        follow(f);
    }

    public boolean ifFollow(int aid,int bid){
        return friendsMapper.ifFollow(aid, bid) > 0;
    }
}
